package com.one.apartmentsystem.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.apartmentsystem.app.domain.User;
import com.one.apartmentsystem.app.dto.LoginDTO;
import com.one.apartmentsystem.app.service.UserService;
import com.one.apartmentsystem.app.mapper.UserMapper;
import com.one.apartmentsystem.common.utils.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author mpk16
 * @description 针对表【user(用户信息表)】的数据库操作Service实现
 * @createDate 2024-08-03 13:39:49
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User login(LoginDTO loginDTO) {

        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getPhone, loginDTO.getPhone()));
        if (user == null) {
            user = new User();
            user.setPhone(loginDTO.getPhone());
            user.setNickname("手机用户");
            user.setPassword("123456");
            user.setStatus("1");
            user.setCreateTime(new java.util.Date());
            user.setLastLoginTime(new java.util.Date());
            user.setModifyTime(new java.util.Date());
            user.setUsername(loginDTO.getPhone());
            userMapper.insert(user);
        } else {
            user.setLastLoginTime(new java.util.Date());
        }

        return user;
    }


    @Override
    public User loginByWx(String openid) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, openid));
        if (user == null) {
            user = new User();
            user.setUsername(openid);
            user.setNickname("微信用户");
            user.setSex("2");
            user.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132");
            user.setCreateTime(new java.util.Date());
            user.setLastLoginTime(new java.util.Date());
            user.setModifyTime(new java.util.Date());
            userMapper.insert(user);
            //获取插入的用户信息的id
        }
        return user;
    }
}




